<?php
class Chuyenbay extends CI_Controller{
    function __construct() {
        parent::__construct();
    }
    function load_ajax1(){
        $this->load->helper('form');
        
        $date_di=$this->input->post('date_di');
        $adult=$this->input->post('adult');
        $children=$this->input->post('$children');
        $from=$this->input->post('from');
        $to=$this->input->post('to');
        $this->load->model('chuyenbay_model');
        $result=$this->chuyenbay_model->load_chuyenbay($date_di,$adult+$children,$from,$to);
        if ($result->num_rows()>0){
            
            $table=$this->make_table_chuyenbay($result->result(),'id="oneway" class="table table-striped table-hover table-chuyenbay"');
            
            $this->load->model('ga_model');
            $ga_di=$this->ga_model->load_by_id($from)->row();            
            $ga_den=$this->ga_model->load_by_id($to)->row();
            
            $page=$this->make_panel($table, $ga_di, $ga_den, $date_di);
            
            echo $page;
        }
        else
        {
            echo '<div id="attention" class="panel panel-danger">
            <div class="panel-heading">Không tìm được chuyến bay nào!</div>
            </div>';
        }
       
    }
    function load_ajax2() {
        $this->load->helper('form');
        
        
        $date_di=$this->input->post('date_di');
        $date_ve=$this->input->post('date_ve');
        $adult=$this->input->post('adult');
        $children=$this->input->post('children');
        $from=$this->input->post('from');
        $to=$this->input->post('to');
        
        
        $this->load->model('chuyenbay_model');
        $result_di=$this->chuyenbay_model->load_chuyenbay($date_di,$adult+$children,$from,$to);
        $result_ve=$this->chuyenbay_model->load_chuyenbay($date_ve,$adult+$children,$to,$from);
        
        
        $this->load->model('ga_model'); 
        $ga_di=$this->ga_model->load_by_id($from)->row(); 
        $ga_den=$this->ga_model->load_by_id($to)->row();
        
        if($result_di->num_rows()<=0 && $result_ve->num_rows()<=0)
        {
            echo '<div id="attention" class="panel panel-danger">
            <div class="panel-heading">Không tìm được chuyến bay đi hoặc về nào!</div>
            </div>';
            return;
        }
        $attributes = array('role' => 'form', 'id' => 'twoway_form',);
        $result= form_open('khachhang/home',$attributes);   
        $result.='<input name="adult" type="hidden" value="'.$adult.'"> <input name="children" type="hidden" value="'.$children.'">';
        
        if ($result_di->num_rows()>0){
            $table_di=$this->make_table_chuyenbay($result_di->result(),'id="twoway_di" class="table table-striped table-chuyenbay"',TRUE,1);
            $result.=$this->make_panel($table_di, $ga_di, $ga_den, $date_di);
        }
        else
        {
            $result.='<input name="r1" type="hidden" value=""><div id="attention" class="panel panel-danger">
            <div class="panel-heading">Không tìm được chuyến bay đi nào!</div>
            </div>';
        }
        
        
        
        if ($result_ve->num_rows()>0){
            $table_ve=$this->make_table_chuyenbay($result_ve->result(),'id="twoway_ve" class="table table-striped table-chuyenbay"',TRUE,2);     
            $result.=$this->make_panel($table_ve, $ga_den, $ga_di, $date_ve);
        }
        else
        {
            $result.='<input name="r2" type="hidden" value=""><div id="attention" class="panel panel-danger">
            <div class="panel-heading">Không tìm được chuyến bay về nào!</div>
            </div>';
        }
        $result.='<input type="submit" style="margin-left: 45%" class="btn btn-primary" value="Continue">'.form_close();
        echo $result;
    }
    function make_table_chuyenbay($data,$attributes=NULL,$radio_btn=FALSE,$table_num=1){
            // generate table data
        $this->load->library('table');
        $this->table->set_empty("&nbsp;");
        $this->table->set_template(array('table_open'=>'<table '.$attributes.'>'));
        $this->table->set_heading('Chuyến bay','Thời gian bay', 'Số Ghế trống','Economy','Business','Saver Flex', 'Máy bay');
        if ($radio_btn===FALSE) {
            foreach ($data as $row) {
                if ($row->ECONOMY==NULL) $row->ECONOMY='Not Available';
                if ($row->BUSINESS==NULL) $row->BUSINESS='Not Available';
                if ($row->SAVER_FLEX==NULL) $row->SAVER_FLEX='Not Available';
                $this->table->add_row($row->MSCB,date_format(date_create_from_format('d-M-y H.i.u a',$row->THOIDIEMDI),'H:i').' - '.date_format(date_create_from_format('d-M-y h.i.u a', $row->THOIDIEMDEN),'H:i'), $row->SOGHETRONG,$row->ECONOMY,$row->BUSINESS,$row->SAVER_FLEX, $row->MODEL.' - '.$row->MSMB);
            }
        }else{
            foreach ($data as $row) {
                if ($row->ECONOMY==NULL) $ECONOMY='Not Available';
                else $ECONOMY='<input type="radio" name="r'.$table_num.'"  value="'.$row->MSCB.'-1">
                               <label for="r'.$table_num.'">'.$row->ECONOMY.'</label>';
                if ($row->BUSINESS==NULL) $BUSINESS='Not Available';
                else $BUSINESS='<input type="radio" name="r'.$table_num.'"  value="'.$row->MSCB.'-2">
                               <label for="r'.$table_num.'">'.$row->BUSINESS.'</label>';
                if ($row->SAVER_FLEX==NULL) $SAVER_FLEX='Not Available';
                else $SAVER_FLEX='<input type="radio" name="r'.$table_num.'"  value="'.$row->MSCB.'-3">
                               <label for="r'.$table_num.'">'.$row->SAVER_FLEX.'</label>';
                $this->table->add_row($row->MSCB,date_format(date_create_from_format('d-M-y H.i.u a',$row->THOIDIEMDI),'H:i').' - '.date_format(date_create_from_format('d-M-y h.i.u a', $row->THOIDIEMDEN),'H:i'), $row->SOGHETRONG,$ECONOMY,$BUSINESS,$SAVER_FLEX, $row->MODEL.' - '.$row->MSMB);
            }
        }
        $table = $this->table->generate();
        return $table;
    }
    function make_panel($data,$ga_di,$ga_den,$date) {
        $panel_head='<div class="panel panel-success">
                    <div class="panel-heading">
                      <h3 class="panel-title">Đi từ <b>'.$ga_di->TENSB.', '.$ga_di->THANHPHO.', '.$ga_di->QUOCGIA.'</b> đến <b>'.$ga_den->TENSB.', '.$ga_den->THANHPHO.', '.$ga_den->QUOCGIA.'</b> ngày <b>'.date_format(date_create_from_format('Y-m-d',$date),'d/m/Y').'</b></h3>
                    </div>
                    <div class="panel-body">';
        $panel_tail='</div></div>';
        return $panel_head.$data.$panel_tail;
    }
    function book1() {
        $MSCB=$this->input->post('MSCB');
        $adult=$this->input->post('adult');
        $children=$this->input->post('children');
        $loai_ve=$this->input->post('loaive');

        $data=$this->make_book_form_data($MSCB, $adult, $children, $loai_ve);
        
        
        $this->make_book_form_oneway($data);
    }
    function book2() {
        $r1=$this->input->post('r1');
        if ($r1!=NULL)
        {
            $r1=  explode('-', $r1);
            $MSCB_di=$r1[0];
            $loai_ve_di=$r1[1];
        }
        
        $r2=$this->input->post('r2');
        if ($r2!=NULL)
        {
            $r2=  explode('-', $r2);
            $MSCB_ve=$r2[0];
            $loai_ve_ve=$r2[1];
        }
        $adult=$this->input->post('adult');
        $children=$this->input->post('children');
        
        if ($r1==NULL && $r2==NULL) return;
        if ($r1==NULL){
            $data=$this->make_book_form_data($MSCB_ve, $adult, $children, $loai_ve_ve);
            $this->make_book_form_oneway($data);
            return;
        }
        if ($r2==NULL){
            $data=$this->make_book_form_data($MSCB_di, $adult, $children, $loai_ve_di);
            $this->make_book_form_oneway($data);
            return;
        }
        
        $data_di=$this->make_book_form_data($MSCB_di, $adult, $children, $loai_ve_di, '_di');
        $data_ve=$this->make_book_form_data($MSCB_ve, $adult, $children, $loai_ve_ve, '_ve');
        $data_di['totalcost']=$data_di['totalcost_di']+$data_ve['totalcost_ve'];
        $this->make_book_form_twoway($data_di, $data_ve);
    }
    function make_book_form_data($MSCB,$adult,$children,$loai_ve,$str_include='') {
        $this->load->model('chuyenbay_model');
        $this->load->model('ga_model');
        $this->load->model('maybay_model');
        $this->load->model('tuyenbay_model');
        $this->load->model('loaimaybay_model');
        
        $chuyenbay=$this->chuyenbay_model->load_by_id($MSCB)->row();
        $tuyenbay=$this->tuyenbay_model->load_by_id($chuyenbay->MSTB)->row();
        $ga_di=$this->ga_model->load_by_id($tuyenbay->MSG_DI)->row();
        $ga_den=$this->ga_model->load_by_id($tuyenbay->MSG_DEN)->row();
        $maybay=$this->maybay_model->load_by_id($chuyenbay->MSMB)->row();
        $loaimaybay=$this->loaimaybay_model->load_by_id($maybay->MSLMB)->row();
        
        $gia_ve=$chuyenbay->ECONOMY;
        $loaive='Economy';
        $ve='PT';
        switch ($loai_ve) {
            case 2:
                $gia_ve=$chuyenbay->BUSINESS;
                $loaive='Business';
                $ve='VIP';
                break;
            case 3:
                $gia_ve=$chuyenbay->SAVER_FLEX;
                $loaive='Saver flex';
                $ve='SAV';
                break;
        }
        
        $this->load->model('chuyenbay_model');
        
        $data['list_ghetrong'.$str_include]=$this->chuyenbay_model->load_ghetrong_loaighe($MSCB,$ve)->result_array();
        
        $data['tuyenbay'.$str_include]='Đi từ <b>'.$ga_di->TENSB.', '.$ga_di->THANHPHO.', '.$ga_di->QUOCGIA.'</b> đến <b>'.$ga_den->TENSB.', '.$ga_den->THANHPHO.', '.$ga_den->QUOCGIA.'</b>';
        
        $data['chuyenbay'.$str_include]=$chuyenbay->MSCB;
        
        $data['thoigian'.$str_include]=date_format(date_create_from_format('d-M-y H.i.u a',$chuyenbay->THOIDIEMDI),'H:i').' - '.date_format(date_create_from_format('d-M-y h.i.u a', $chuyenbay->THOIDIEMDEN),'H:i');
    
        $data['maybay'.$str_include]=$loaimaybay->MODEL.' - '.$chuyenbay->MSMB;
        
        $data['loaive'.$str_include]=$loaive;
        
        $data['maloaive'.$str_include]=$loai_ve;
        
        $data['adult']=$adult;
        
        $data['children']=$children;
        
        $data['ngaybay'.$str_include]= date_format(date_create_from_format('d-M-y H.i.u a',$chuyenbay->THOIDIEMDI),'d/m/Y');
        
        $data['totalcost'.$str_include]=$gia_ve*($adult+$children);
        
        return $data;
    }
    function make_book_form_oneway($data) {
        $this->load->helper('form');
        echo $this->load->view('/khachhang/oneway_form_book_trip',$data,TRUE);
    }
    function make_book_form_twoway($data_di,$data_ve) {
        $this->load->helper('form');
        $data=array_merge($data_di,$data_ve);
        echo $this->load->view('/khachhang/twoway_form_book_trip',$data,TRUE);
    } 
    function add_khachhang($MSCB,$way=''){
        $this->load->helper('string');
        $this->load->model('khachhang_model');
        $this->load->model('phivchh_model');
        $this->load->model('ghekhach_model');
        
        $adult=$this->input->post('adult');
        $children=$this->input->post('children');
        $loaive=$this->input->post('loaive');
        
        $MSPHH=$this->phivchh_model->load_by_loaive($loaive)->row()->MSPHH;
        
        $adult_inserted=NULL;
        $children_inserted=NULL;
        
        if ($adult>0){
            for ($i=0;$i<$adult;$i++){
                $adult_arr1['MSKH']='NL';
                $adult_arr1['RAND_CODE']=random_string('alnum', 20);
                $adult_arr1['HOTEN']=$this->input->post('fullname_nl'.$i);
                $adult_arr1['NGAYSINH']=$this->input->post('ngaysinh_nl'.$i);
                $adult_arr1['GIOITINH']=$this->input->post('gioitinh_nl'.$i);
                $adult_arr1['DIACHI']=$this->input->post('diachi_nl'.$i);
                $adult_arr1['QUOCTICH']=$this->input->post('quocgia_nl'.$i);
                $adult_arr1['MSCB']=$MSCB;
                $adult_arr1['SODT']=$this->input->post('sdt_nl'.$i);
                $adult_arr1['MSPHH']=$MSPHH;
                $adult_arr2['CMND']=$this->input->post('CMND_nl'.$i);
                $adult_arr2['PASSPORT']=$this->input->post('passport_nl'.$i);
                $ghe_so=$this->input->post('soghe_nl'.$way.$i);
                
                $inserted=$this->khachhang_model->insert_KH($adult_arr1,$adult_arr2,'KHACHHANGNL');
                $this->ghekhach_model->insert($inserted['MSKH'],$inserted['MSCB'],$ghe_so);
                $inserted['GHESO']=$ghe_so;
                $adult_inserted[]=$inserted;
            }
        }
        if ($children>0){
            for ($i=0;$i<$children;$i++){
                $children_arr1['MSKH']='TE';
                $children_arr1['RAND_CODE']=random_string('alnum', 20);
                $children_arr1['HOTEN']=$this->input->post('fullname_te'.$i);
                $children_arr1['NGAYSINH']=$this->input->post('ngaysinh_te'.$i);
                $children_arr1['GIOITINH']=$this->input->post('gioitinh_te'.$i);
                $children_arr1['DIACHI']=$this->input->post('diachi_te'.$i);
                $children_arr1['QUOCTICH']=$this->input->post('quocgia_te'.$i);
                $children_arr1['MSCB']=$MSCB;
                $children_arr1['SODT']=$adult_inserted[$this->input->post('MSNGH_te'.$i)]['SODT'];
                $children_arr1['MSPHH']=$MSPHH;
                $children_arr2['THONGTINKSINH']=$this->input->post('thongtinks_te'.$i);
                $children_arr2['MSNGH']=$adult_inserted[$this->input->post('MSNGH_te'.$i)]['MSKH'];
                $ghe_so=$this->input->post('soghe_te'.$way.$i);
                
                $inserted=$this->khachhang_model->insert_KH($children_arr1,$children_arr2,'KHACHHANGTE');
                $this->ghekhach_model->insert($inserted['MSKH'],$inserted['MSCB'],$ghe_so);
                $inserted['GHESO']=$ghe_so;
                $children_inserted[]=$inserted;
            }
        }
        $result=array('adult_data'=>$adult_inserted,'children_data'=>$children_inserted);
        return $result;
    }
    function submit1() {
        $MSCB=$this->input->post('MSCB');
        $data=$this->add_khachhang($MSCB);
        $loaive=$this->input->post('loaive');
        $data['adult']=count($data['adult_data']);
        $data['children']=count($data['children_data']);
        
        $chuyenbay_info=$this->make_book_form_data($MSCB, $data['adult'], $data['children'], $loaive);
        $data=array_merge($data,$chuyenbay_info);
        echo $this->load->view('khachhang/success_page',$data);
    }
    function submit2(){
        
        //chuyen bay di
        $MSCB=$this->input->post('MSCB_di');
        
        $data=$this->add_khachhang($MSCB,'_di');
        $loaive=$this->input->post('loaive_di');
        $data['adult']=count($data['adult_data']);
        $data['children']=count($data['children_data']);
        
        $chuyenbay_info=$this->make_book_form_data($MSCB, $data['adult'], $data['children'], $loaive);
        
        
        $data=array_merge($data,$chuyenbay_info);
        
        $totalcost_di=$data['totalcost'];
        
        $result_page=$this->load->view('khachhang/success_page',$data,TRUE);  
        $data=NULL;
        //chuyen bay ve
        $MSCB=$this->input->post('MSCB_ve');
        
        $data=$this->add_khachhang($MSCB,'_ve');
        $loaive=$this->input->post('loaive_ve');
        $data['adult']=count($data['adult_data']);
        $data['children']=count($data['children_data']);
        
        $chuyenbay_info=$this->make_book_form_data($MSCB, $data['adult'], $data['children'], $loaive);
        
        
        $data=array_merge($data,$chuyenbay_info);
        
        $totalcost_ve=$data['totalcost'];
        
        $result_page.=$this->load->view('khachhang/success_page',$data,TRUE);
        
        $result_page.='<div class="well">
                            <h4>Tổng số tiền: '.($totalcost_di+$totalcost_ve).'</h4>
                        </div>';
        echo $result_page;
    }
}